using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class TopicFileDal : Base
    {
        public TopicFileDal() : base() { }

        public enum Fields
        {
            Id,
            TopicId,
            FileId,
            Name,
            Size,
            FileType,
            ContentType,
            Extension,
            Width,
            Height,
            VersionId
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.TopicId, reader, table);
            GetReaderValue<int>(Fields.FileId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<long>(Fields.Size, reader, table);
            GetReaderValue<int>(Fields.FileType, reader, table);
            GetReaderValue<string>(Fields.ContentType, reader, table);
            GetReaderValue<string>(Fields.Extension, reader, table);
            GetReaderValue<int>(Fields.Width, reader, table);
            GetReaderValue<int>(Fields.Height, reader, table);
        }

        public Hashtable GetByTopicIdAndName(int topicId, string name)
        {
            SqlCommand cmd = CreateCommand("GetByTopicIdAndName");
            AddInputParameter(cmd, "TopicId", topicId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByTopicId(int topicId)
        {
            SqlCommand cmd = CreateCommand("GetAllByTopicId");
            AddInputParameter(cmd, "TopicId", topicId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

        public void Update(ref int id, int topicId, int fileId, string name, ref long size, ref int fileType, ref string contentType, ref string extension, ref int width, ref int height, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "TopicId", topicId, SqlDbType.Int);
            AddInputParameter(cmd, "FileId", fileId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "SizeOut", SqlDbType.BigInt);
            AddOutputParameter(cmd, "FileTypeOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ContentTypeOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "ExtensionOut", SqlDbType.VarChar, 6);
            AddOutputParameter(cmd, "WidthOut", SqlDbType.Int);
            AddOutputParameter(cmd, "HeightOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            size = GetOutputValue<long>(cmd, "SizeOut");
            fileType = GetOutputValue<int>(cmd, "FileTypeOut");
            contentType = GetOutputValue<string>(cmd, "ContentTypeOut");
            extension = GetOutputValue<string>(cmd, "ExtensionOut");
            width = GetOutputValue<int>(cmd, "WidthOut");
            height = GetOutputValue<int>(cmd, "HeightOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

    }
}
